<?php

class Exam_Api_Faq extends Core_Api_Abstract {
	/**
     * Get FAQ Select
     * @param array $params
     * @return Zend_Db_Select
     */
    public function getFAQSelect($params = array ()) {
    	$faqTable = Engine_Api::_()->getDbTable('faqs', 'exam');
    	$faqCateTable = Engine_Api::_()->getDbTable('faqcategories', 'exam');
    	$select = $faqTable->select()
    		->setIntegrityCheck(false)
    		->from(
    			array (
    				"faq"	=>	$faqTable->info('name')
    			)
    		)
    		->join(
	    		array (    		
	    			"cat"	=>	$faqCateTable->info('name')    		
	    		),
	    		'faq.faqcategory_id = cat.faqcategory_id',
	    		array (
	    			'faqcategory_name'
	    		)
    		);
    	
    	// Faq id
    	if (!empty($params['id'])) {
			if (is_numeric($params['id'])) {
				$select->where('faq.faq_id = ?', intval($params['id']));
			}
			elseif (is_array($params['id'])) {
				$select->where('faq.faq_id IN (?)', $params['id']);
			}
		}
		
		// Category id
    	if (!empty($params['categoryId'])) {
			if (is_numeric($params['categoryId'])) {
				$select->where('faq.faqcategory_id = ?', intval($params['categoryId']));
			}
			elseif (is_array($params['categoryId'])) {
				$select->where('faq.faqcategory_id IN (?)', $params['categoryId']);
			}
		}
		
		// Question
    	if (!empty($params['question'])) {
			$select->where('faq.question LIKE ?', '%' . $params['question'] . '%');
		}
		
		// Status
    	if (!empty($params['status'])) {
			if (is_numeric($params['status'])) {
				$select->where('faq.status = ?', intval($params['status']));
			}
			elseif (is_array($params['status'])) {
				$select->where('faq.status IN (?)', $params['status']);
			}
		}
		
    	// Other where
		if (!empty($params['where']) && is_array($params['where'])) {
			foreach ($params['where'] as $k => $v) {
				if (is_int($k)) {
					$select->where($v);
				}
				else {
					$select->where($k, $v);
				}
			}
		}
		
		// Order
		if (!empty($params['orderBy'])) {
			$select->order($params['orderBy']);
		}
		
		return $select;
    }
    
    /**
     * Get FAQ Paginator
     * @param array $params
     * @return Zend_Paginator
     */
    public function getFAQPaginator($params = array ()) {
    	return Zend_Paginator::factory($this->getFAQSelect($params));
    }
}
